package com.grts.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.grts.pojo.UserBean;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		System.out.println("拦截器进入");
		HttpSession session=request.getSession();
		UserBean user=(UserBean) session.getAttribute("user");
		if(user!=null)
		{
			return true;
		}
		session.setAttribute("errorMsg", "请先登录");
		PrintWriter pw = response.getWriter();
		pw.write("<script>top.location.href ='../login.jsp';</script>");
		pw.flush();
		pw.close();
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("拦截器进入2");
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("拦截器进入3");
	}

}
